package com.inspinia.message.rpc.service;

import cn.hutool.core.collection.CollectionUtil;
import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.pagination.Pager;
import com.inspinia.base.service.BaseServiceImpl;
import com.inspinia.message.common.model.Notice;
import com.inspinia.message.common.model.NoticePush;
import com.inspinia.message.common.model.NoticePushStatus;
import com.inspinia.message.common.model.NoticePushVo;
import com.inspinia.message.dao.NoticePushDao;
import com.inspinia.message.rpc.api.NoticePushService;
import com.inspinia.message.rpc.api.NoticeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 通知推送的服务层实现类
 * Created by heshun on 2018-1-8.
 */
@Service
public class NoticePushServiceImpl extends BaseServiceImpl<NoticePush> implements NoticePushService {
    @Resource
    private NoticePushDao noticePushDao;
    @Resource
    private NoticeService noticeService;

    @Override
    protected BaseDao<NoticePush> getDao() {
        return noticePushDao;
    }

    @Override
    public List<Long> batchInsert(Long noticeId, Collection<Long> userIds) {
        if (CollectionUtil.isNotEmpty(userIds)) {
            List<NoticePush> noticePushes = new ArrayList<>(userIds.size());
            userIds.forEach(userId -> noticePushes.add(new NoticePush(noticeId, userId, NoticePushStatus.unread)));

            noticePushDao.batchInsert(noticePushes);

            List<Long> ids = new ArrayList<>(userIds.size());
            noticePushes.forEach(n -> ids.add(n.getId()));
            return ids;
        }
        return new ArrayList<>(0);

    }

    @Override
    public int countUnread(Long userId) {
        return noticePushDao.getUnreadNumber(userId);
    }

    @Transactional
    @Override
    public void setToRead(Long id) {
        noticePushDao.setToRead(id);
    }

    @Override
    public List<NoticePush> findUnread(Long userId) {
        return noticePushDao.findUnread(userId);
    }

    @Override
    public Pager<Notice> findNotices(NoticePushVo vo) {
        Pager page = findPage(vo);
        List<Notice> notices = new ArrayList<>(page.getList().size());
        for (NoticePush noticePush : (List<NoticePush>) page.getList()) {
            Notice notice = noticeService.get(noticePush.getNoticeId());
            notice.setNoticePushId(noticePush.getId());
            notice.setAddresseeId(noticePush.getAddresseeId());
            notice.setNoticePushStatus(noticePush.getStatus());
            notice.setReadTime(noticePush.getReadTime());
            notices.add(notice);
        }
        page.setList(notices);
        return page;
    }

    @Override
    public Pager<Notice> listUnread(NoticePushVo vo) {
        vo.setStatus(NoticePushStatus.unread);
        return findNotices(vo);
    }

    @Transactional
    @Override
    public void batchSetToRead(Long[] ids) {
        noticePushDao.batchSetToRead(ids);
    }

    @Override
    public void deleteDayBefore(int day) {
        noticePushDao.deleteDayBefore(day);
    }

    @Override
    public List<NoticePush> findTopNUnreadNoticeId(Long userId, int topN) {
        return noticePushDao.findTopNUnreadNoticeId(userId, topN);
    }

    @Override
    public void readAll(Long userId) {
        noticePushDao.readAll(userId);
    }
}
